import java.awt.*; 
import javax.swing.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class GridFrame extends JFrame
{
  private static final long serialVersionUID = 1L;
	
  public static final int BLOCS = 100;
  public static final int WIDTH = P4.WIDTH;
  public static final int HEIGHT = P4.HEIGHT;
  public static final int EMPTY = P4.EMPTY;
  public static final int PLAYER1 = P4.PLAYER1;
  public static final int PLAYER2 = P4.PLAYER2;

  private ImageIcon red = null;
  private ImageIcon yellow = null;
  private ImageIcon empty = null;

  private JLabel[][] labels = new JLabel[HEIGHT][WIDTH];

  public GridFrame()
  {
    super();
    build();  
  }
    
  private void build()
  {
    setTitle("Puissance 4");
    setSize(WIDTH*BLOCS,HEIGHT*BLOCS);
    setLocationRelativeTo(null);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setContentPane(buildContentPane());
  }

  private JPanel buildContentPane()
  {
    JPanel panel = new JPanel(new GridLayout(HEIGHT, WIDTH, 1, 1));
    empty = new ImageIcon(Toolkit.getDefaultToolkit().getImage("images/empty.jpg").getScaledInstance(BLOCS, BLOCS, Image.SCALE_DEFAULT));
    red = new ImageIcon(Toolkit.getDefaultToolkit().getImage("images/red.jpg").getScaledInstance(BLOCS, BLOCS, Image.SCALE_DEFAULT));
    yellow = new ImageIcon(Toolkit.getDefaultToolkit().getImage("images/yellow.jpg").getScaledInstance(BLOCS, BLOCS, Image.SCALE_DEFAULT));
    for(int i=0; i<HEIGHT; i++)
      {
	for(int j=0; j<WIDTH; j++)
	  {
	    labels[i][j] = new JLabel();
	    labels[i][j].setIcon(empty); 
	    labels[i][j].setPreferredSize(new Dimension(BLOCS, BLOCS));
	    panel.add(labels[i][j]);
	  }
      }
    return panel;
  }
    
  public void setIcon(int x, int y, int joueur)
  {
    if(joueur==PLAYER1)
      labels[HEIGHT-1-x][y].setIcon(red);
    else
      labels[HEIGHT-1-x][y].setIcon(yellow);
  }
}